package controller;

import dao.StudentDao;
import domain.StudentDomain;
import servcie.StudentService;

import java.util.Scanner;

public class StudentController {
    //开启学生管理系统，并展示学生管理系统菜单
    private Scanner scanner=new Scanner(System.in);
    private  StudentService studentService=new StudentService();
    public void start() {
        studentlo:
       while (true){
           System.out.println("----欢迎来到学生管理信息系统----");
           System.out.println("请输入你的选择：1、添加学生 2、删除学生 3、修改学生  4、查询学生  5、退出");
           String choice=scanner.next();
           switch (choice){
               case "1":
//                   System.out.println("添加学生");
                   addStudent();
                   break;
               case "2":
                  deletStudent();
                   break;
               case "3":
                   changeStudent();
                   break;
               case "4":
                   findAllStuden();
                   break;
               case "5":
                   System.out.println("退出");
                   break studentlo;
               default:
                   System.out.println("你输入有误，重新输入");
           }
       }

    }

    private void changeStudent() {
        System.out.println("请输入要修改学生信息的学号：");
        String id=scanner.next();
        boolean exists = studentService.isExists(id);
        if(exists){
            boolean flag= studentService.changeStudent(id);
            if (flag) System.out.println("修改成功");
            else System.out.println("修改失败");
        }else System.out.println(id+"该学号不存在！");

    }

    private void findAllStuden() {
        StudentDao dao=new StudentDao();
        StudentDomain[] allStudent = dao.findAllStudent();
        for (int i = 0; i < allStudent.length; i++) {
            StudentDomain domain = allStudent[i];
            if(domain==null){
                if(i==0){
                    System.out.println("请先添加信息在打印！");
                    break;
                }else {
                    System.out.println("打印了"+i+"条信息");
                    break;
                }

            }else System.out.println(domain.getId()+"..."+domain.getName()+"..."+domain.getAge());


        }

    }

    private void deletStudent() {
        System.out.println("请输入要删除学生的学号：");
        String id=scanner.next();
        boolean exists = studentService.isExists(id);
        if(exists){
           boolean flag= studentService.deletStudent(id);
           if (flag) System.out.println("删除成功");
           else System.out.println("删除失败");
        }else System.out.println(id+"该学号不存在！");


    }

    private void addStudent() {

        //1、键盘接受学生信息
        String id;
        while (true){
         System.out.println("请输入学生的学号：");
         id=scanner.next();
         boolean flag= studentService.isExists(id);
         if (flag) System.out.println("学好已被占用，请重新输入");
         else break;
     }
        System.out.println("请输入学生的姓名：");
        String name=scanner.next();
        System.out.println("请输入学生的年龄：");
        int age=scanner.nextInt();
        //2、将学生信息封装为学生对象
        StudentDomain stu=new StudentDomain();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        //3、将学生对象传递给StudentService（业务员）中的addStudent
        boolean result=studentService.addStudent(stu);
        //4、根据返回的boolean类型结果，在控制台打印成功\失败
        if(result){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
     }

     private void findAll(){
        //调用业务员中的获取方法，得到学生的对象数组
         StudentDomain [] domains=studentService.findAll();
         //判断数组的内存地址，是否为null
         if(domains==null){
             System.out.println("查无信息，请添加后重试");
             return;
         }else {
             //遍历数组，获取学生信息并打印在控制台
             for (int i = 0; i < domains.length; i++) {
                 StudentDomain domain=domains[i];
                 if(domain!=null){
                     System.out.println(domain.getId()+"..."+domain.getName()+"..."+domain.getAge());
                 }
             }
         }


     }

}
